home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q1082.dms / q1082.adf / src.lzh / Fig / text.c < prev    next >
C/C++ Source or Header  |  1991-07-18  |  6KB  |  237 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : Aug 1985.
  7.  *    2nd revision : Feb 1988.
  8.  *
  9.  *    %W%    %G%
  10. */
  11. #include "fig.h"
  12. #include "resources.h"
  13. #include "alloc.h"
  14. #include "const.h"
  15. #include "font.h"
  16. #include "func.h"
  17. #include "object.h"
  18. #include "paintop.h"
  19.  
  20. extern            (*canvas_kbd_proc)();
  21. extern            (*canvas_locmove_proc)();
  22. extern            (*canvas_leftbut_proc)();
  23. extern            (*canvas_middlebut_proc)();
  24. extern            (*canvas_rightbut_proc)();
  25. extern            null_proc();
  26. extern            set_popupmenu();
  27. extern F_text        *text_search();
  28.  
  29. extern            char_handler();
  30. extern int        cur_x, cur_y;
  31. extern char        prefix[], suffix[];
  32. extern int        leng_prefix, leng_suffix;
  33. extern int        cur_color;
  34. extern int        cur_textstyle;
  35. extern float        cur_angle;
  36.  
  37. extern F_compound    objects;
  38.  
  39. static int        char_ht, char_wid;
  40. static int        base_x, base_y;
  41. static PR_SIZE        tsize;
  42. static F_text        *cur_text;
  43.  
  44. F_text            *create_text();
  45. static            finish_text_input();
  46. static            finish_n_start();
  47. static            init_text_input();
  48.  
  49. static
  50. finish_n_start(x, y)
  51. {
  52.     wrap_up();
  53.     init_text_input(x, y);
  54.     }
  55.  
  56. static
  57. finish_text_input()
  58. {
  59.     wrap_up();
  60.     text_drawing_selected();
  61.     }
  62.  
  63. static
  64. wrap_up()
  65. {
  66.     PR_SIZE        size;
  67.     PR_SIZE pf_textwidth();
  68.     int        kbd_received;
  69.  
  70.     reset_action_on();
  71.     kbd_received = terminate_char_handler();
  72.     if ( ! kbd_received) return;
  73.  
  74.     if (cur_text == NULL) {    /* a brand new text */
  75.         if (leng_prefix == 0) return;
  76.         pw_text(canvas_pixwin, base_x, base_y, PAINT, canvas_font, prefix);
  77.         cur_text = create_text();
  78.         insert_text(&objects.texts, cur_text);
  79.         }
  80.     else {            /* existing text modified */
  81.         strcat(prefix, suffix);
  82.         leng_prefix += leng_suffix;
  83.         if (leng_prefix == 0) {
  84.         delete_text(&objects.texts, cur_text);
  85.         cfree(cur_text->cstring);
  86.         free((char*)cur_text);
  87.         return;
  88.         }
  89.         if (strlen(cur_text->cstring) >= leng_prefix) { 
  90.         strcpy(cur_text->cstring, prefix);
  91.         }
  92.         else { /* free old and allocate new */
  93.         cfree(cur_text->cstring);
  94.         cur_text->cstring = malloc((unsigned)(leng_prefix+1));
  95.         strcpy(cur_text->cstring, prefix);
  96.         }
  97.         size = pf_textwidth(leng_prefix, canvas_font, prefix);
  98.         cur_text->height = size.y;
  99.         cur_text->length = size.x;  /* in pixels */
  100.         }
  101.     clean_up();
  102.     set_action_object(F_TEXT, O_TEXT);
  103.     set_latesttext(cur_text);
  104.     set_modifiedflag();
  105.     }
  106.  
  107. static
  108. init_text_input(x, y)
  109. int    x, y;
  110. {
  111.     cur_x = x;
  112.     cur_y = y;
  113.  
  114.     set_action_on();
  115.     canvas_kbd_proc = char_handler;
  116.     canvas_middlebut_proc = finish_text_input;
  117.     canvas_leftbut_proc = finish_n_start;
  118.     canvas_rightbut_proc = null_proc;
  119.  
  120.     if ((cur_text = text_search(cur_x, cur_y)) == NULL) {
  121.         leng_prefix = leng_suffix = 0;
  122.         *suffix = 0;
  123.         prefix[leng_prefix] = '\0';
  124.         base_x = cur_x;
  125.         base_y = cur_y;
  126.         }
  127.     else { /* leng_prefix is # of char in the text before the cursor */
  128.         leng_suffix = strlen(cur_text->cstring);
  129.         leng_prefix = prefix_length(roman_font, cur_text->cstring, 
  130.                     cur_x - cur_text->base_x);
  131.         leng_suffix -= leng_prefix;
  132.         cpy_n_char(prefix, cur_text->cstring, leng_prefix);
  133.         strcpy(suffix, &cur_text->cstring[leng_prefix]);
  134.         tsize = pf_textwidth(leng_prefix, canvas_font, prefix);
  135.         cur_x = base_x = cur_text->base_x;
  136.         cur_y = base_y = cur_text->base_y;
  137.         cur_x += tsize.x;
  138.         }
  139.     initialize_char_handler(canvas_pixwin, canvas_font,
  140.             finish_text_input, base_x, base_y);
  141.     }
  142.  
  143. text_drawing_selected()
  144. {
  145.     canvas_kbd_proc = null_proc;
  146.     canvas_locmove_proc = null_proc;
  147.     canvas_middlebut_proc = null_proc;
  148.     canvas_leftbut_proc = init_text_input;
  149.     canvas_rightbut_proc = set_popupmenu;
  150.     char_ht = char_height(canvas_font);
  151.     char_wid = char_width(canvas_font);
  152.     set_cursor(&pencil_cursor);
  153.     }
  154.  
  155. F_text *
  156. create_text()
  157. {
  158.     F_text        *text;
  159.     PR_SIZE        size;
  160.  
  161.     if ((Text_malloc(text)) == NULL) {
  162.         put_msg(Err_mem);
  163.         return(NULL);
  164.         }
  165.     text->cstring = malloc((unsigned)(leng_prefix+1));
  166.     text->type = T_LEFT_JUSTIFIED;
  167.     text->font = 0;
  168.     text->angle = cur_angle;
  169.     text->style = cur_textstyle;
  170.     text->color = cur_color;
  171.     text->depth = 0;
  172.     text->pen = NULL;
  173.     size = pf_textwidth(leng_prefix, canvas_font, prefix);
  174.     text->size = size.y;
  175.     text->length = size.x;    /* in pixels */
  176.     text->height = size.y;    /* in pixels */
  177.     text->base_x = base_x;
  178.     text->base_y = base_y;
  179.     strcpy(text->cstring, prefix);
  180.     text->next = NULL;
  181.     return(text);
  182.     }
  183.  
  184. cpy_n_char(dst, src, n)
  185. char    *dst, *src;
  186. int     n;
  187. {
  188.     /* src must be longer than n chars */
  189.  
  190.     while (n--) *dst++ = *src++;
  191.     *dst = '\0';
  192.     }
  193.  
  194. int
  195. prefix_length(font, string, where_p)
  196. PIX_FONT        font;
  197. char        *string;
  198. int         where_p;
  199. {
  200.     /* c stands for character unit and p for pixel unit */
  201.     int        l, len_c, len_p;
  202.     int        char_wid, where_c;
  203.     PR_SIZE        size;
  204.  
  205.     if (font == NULL) font = roman_font;
  206.     len_c = strlen(string);
  207.     size = pf_textwidth(len_c, font, string);
  208.     len_p = size.x;
  209.     if (where_p >= len_p) return(len_c); /* entire string is the preffix */
  210.  
  211.     char_wid = char_width(font);
  212.     where_c = where_p / char_wid;    /* estimated char position */
  213.     size = pf_textwidth(where_c, font, string);
  214.     l = size.x;    /* actual lenght (pixels) of string of where_c chars */
  215.     if (l < where_p) {
  216.         do {    /* add the width of next char to l */
  217.         l += (char_wid = char_advance(font,string[where_c++]));
  218.         } while (l < where_p);
  219.         if (l-(char_wid>>1) >= where_p) where_c--;
  220.         }
  221.     else if (l > where_p) {
  222.         do {    /* subtract the width of last char from l */
  223.         l -= (char_wid = char_advance(font,string[--where_c]));
  224.         } while (l > where_p);
  225.         if (l+(char_wid>>1) >= where_p) where_c++;
  226.         }
  227.     return(where_c);
  228.     }
  229.  
  230. draw_text(text, op)
  231. F_text    *text;
  232. int    op;
  233. {
  234.     pw_text(canvas_pixwin, text->base_x, text->base_y, 
  235.         op, canvas_font, text->cstring);
  236.     }
  237.